package com.mindata.blockchain.block.check;

import com.mindata.blockchain.block.Block;
import com.mindata.blockchain.socket.body.RpcCheckBlockBody;
import com.mindata.blockchain.socket.body.RpcCheckBlockResult;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 区块校验
 *
 * @author wuweifeng wrote on 2018/3/14.
 */
@Component
public class CheckerManager {
    @Resource
    private BlockChecker blockChecker;

    /**
     * 基本校验
     *
     * @param block block
     * @return 校验结果
     */
    public RpcCheckBlockBody check(Block block) {
        int code = blockChecker.checkSign(block);
        if (code != 0) {
            return new RpcCheckBlockBody(RpcCheckBlockResult.INVALID_SIGN);
        }
        int number = blockChecker.checkNum(block);
        if (number != 0) {
            return new RpcCheckBlockBody(RpcCheckBlockResult.INVALID_NUMBER);
        }
        int time = blockChecker.checkTime(block);
        if (time != 0) {
            return new RpcCheckBlockBody(RpcCheckBlockResult.INVALID_TIME);
        }
        int hash = blockChecker.checkHash(block);
        if (hash != 0) {
            return new RpcCheckBlockBody(RpcCheckBlockResult.INVALID_HASH);
        }
        int permission = blockChecker.checkPermission(block);
        if (permission != 0) {
            return new RpcCheckBlockBody(RpcCheckBlockResult.INVALID_AUTH);
        }
        int instruction = blockChecker.checkBlockInstruction(block);
        if (instruction != 0) {
            return new RpcCheckBlockBody(RpcCheckBlockResult.EXITS_INSTRUCTION);
        }

        return new RpcCheckBlockBody(RpcCheckBlockResult.SUCCESS);
    }

}
